<?php

namespace App\Models;

class OrderParcel extends BaseModel
{
    protected $table = 'order_parcel';

    protected $fillable = [
        'user_id',
        'order_id',
        'title',
        'type',
        'pack_type',
        'long',
        'width',
        'height',
        'weight',
        'long_all',
        'width_all',
        'height_all',
        'weight_all',
        'num',
        'pack',
        'container',
        'declared_value',
    ];

    protected $appends = ['pack_type_att'];

    public static $EnumPackType = [
        0 => '单件体积/重', 1 => '总体积/重', 2 => '整柜'
    ];

    public function getPackTypeAttAttribute()
    {
        return self::$EnumPackType[$this->pack_type] ?? $this->pack_type;
    }


    public function user()
    {
        return $this->hasOne('App\Models\User', 'id', 'user_id');
    }

    public static function getPrice($data, $forwarder_line_id){
        $weight_all = $data['type'] > 0 ? $data['weight_all'] : $data['weight'] * $data['num'];
        $cost = ForwarderLineCost::where('forwarder_line_id',$forwarder_line_id)
            ->where('cargo', $data['type'])
            ->where('min_weight', '<=', $weight_all)
            ->where('max_weight', '>', $weight_all)
            ->first();
        if(!is_object($cost)){
            abort(5061);
        }
        return $cost->amount;
    }
}
